package org.come.until;

import java.math.BigDecimal;

import org.come.Frame.AircraftJframe;
import org.come.Frame.MountJframe;
import org.come.Frame.MountSkillsJframe;
import org.come.Frame.PartnerJframe;
import org.come.Frame.RoleToggleJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.*;
import org.come.bean.LoginResult;
import org.come.entity.Fly;
import org.come.entity.Goodstable;
import org.come.entity.Mount;
import org.come.entity.RoleSummoning;
import org.come.model.Lingbao;
import org.come.mouslisten.GoodsMouslisten;
import org.come.mouslisten.PetAddPointMouslisten;

import com.tool.btn.NeidanBtn;
import com.tool.image.ImageMixDeal;
import com.tool.pet.PetProperty;
import com.tool.role.ExpUtil;
import com.tool.role.GetExp;
import com.tool.role.RoleData;
import com.tool.role.RoleLingFa;
import com.tool.role.RoleProperty;

public class ExpIncreaseUntil {
	/**修改经验的处理*/
	public static void RoleExp(LoginResult loginResult,String v){
		String[] vs=v.split("=");
		int hp=loginResult.getHp().intValue();
		int mp=loginResult.getMp().intValue();
		if (vs.length>=4) {hp=Integer.parseInt(vs[2]);mp=Integer.parseInt(vs[3]);} 
		if (hp==0) {hp=RoleProperty.getHp(loginResult);mp=RoleProperty.getMp(loginResult);}
		loginResult.setHp(new BigDecimal(hp));
		loginResult.setMp(new BigDecimal(mp));
		loginResult.setExperience(new BigDecimal(vs[1]));
		int lvlN=Integer.parseInt(vs[0].substring(1));
		if (vs.length>4) {
			loginResult.setGrade(lvlN);
			ImageMixDeal.userimg.getRoleShow().setGrade(lvlN);
			for (int i = 4; i < vs.length; i++) {
				if (i==4) {loginResult.setBone(Integer.parseInt(vs[i]));}
				else if (i==5) {loginResult.setSpir(Integer.parseInt(vs[i]));}
				else if (i==6) {loginResult.setPower(Integer.parseInt(vs[i]));}
				else if (i==7) {loginResult.setSpeed(Integer.parseInt(vs[i]));}
				else if (i==8) {loginResult.setCalm(Integer.parseInt(vs[i]));}
			}
		}else {
			int lvlY=loginResult.getGrade();
			if (lvlN!=lvlY) {
				loginResult.setGrade(lvlN);
				ImageMixDeal.userimg.getRoleShow().setGrade(lvlN);
				int cha=lvlN-lvlY;
				loginResult.setBone(loginResult.getBone()+cha);
			    loginResult.setSpir(loginResult.getSpir()+cha);
			    loginResult.setPower(loginResult.getPower()+cha);
			    loginResult.setSpeed(loginResult.getSpeed()+cha);
				if (loginResult.getTurnAround()>=4) {
				    loginResult.setCalm(loginResult.getCalm()+cha);
				}
				RoleProperty.Resetgrade(loginResult.getGrade(), loginResult.getRace_id());
				//人物的状态回满
				RoleData.getRoleData().getLoginResult().setHp(new BigDecimal(RoleProperty.getHp(RoleData.getRoleData().getLoginResult()))); 
				RoleData.getRoleData().getLoginResult().setMp(new BigDecimal(RoleProperty.getMp(RoleData.getRoleData().getLoginResult())));
				PetAddPointMouslisten.getplayerValue();
			}	
		}
		/**刷新伙伴面板等级*/
        if (FormsManagement.getInternalForm2(105) != null) {
            if(FormsManagement.getframe(105).isVisible()){
                PartnerMainJpanel partnerMainJpanel = PartnerJframe.getPartnerJframe().getPartnerMainJpanel();
                partnerMainJpanel.refreshLvl();
            }
        }
		Article.manxie(loginResult);
	}
	public static void PetExp(String v){//召唤兽ID=等级=经验=亲密=HP=MP=忠诚
		String[] vs=v.split("=");
		RoleSummoning pet=getPet(new BigDecimal(vs[0].substring(1)));
		if (pet==null) {
			return;
		}
		int lvlY=pet.getGrade();
		int lvlN=Integer.parseInt(vs[1]);
		pet.setGrade(lvlN);
		pet.setExp(new BigDecimal(vs[2]));
		pet.setFriendliness(Long.parseLong(vs[3]));
		if (lvlN!=lvlY) {
			int cha=lvlN-lvlY;
			pet.setBone(pet.getBone() + cha);
			pet.setSpir(pet.getSpir() + cha);
			pet.setPower(pet.getPower() + cha);
			pet.setSpeed(pet.getSpeed() + cha);
			pet.setFaithful(100);
			if (pet.getTurnRount()>=4) {
				pet.setCalm(pet.getCalm()+cha);
			}
			int[] pets=PetProperty.getPetHMASp(pet);
			pet.setBasishp(pets[0]);
			pet.setBasismp(pets[1]);


			SendRoleAndRolesummingUntil.sendRoleSumming(pet);
		}else {
			int hp=Integer.parseInt(vs[4]);
			int mp=Integer.parseInt(vs[5]);
			if (hp==0) {
				int[] pets=PetProperty.getPetHMASp(pet);
				pet.setBasishp(pets[0]);
				pet.setBasismp(pets[1]);
			}else {
				pet.setBasishp(hp);
				pet.setBasismp(mp);
			}
			if(vs.length > 6) {
				pet.setFaithful(Integer.parseInt(vs[6]));
			}
		}
		if (UserMessUntil.getChosePetMes()!=null&&pet.getSid().compareTo(UserMessUntil.getChosePetMes().getSid())==0) {
			PetAddPointMouslisten.showPetValue();
		}
		if (RoleData.getRoleData().getLoginResult().getSummoning_id()!=null&&pet.getSid().compareTo(RoleData.getRoleData().getLoginResult().getSummoning_id())==0) {
			Article.souxie(pet);
			//WhuJpanel.souxies(pet);
		}
	}
	public static void MountExp(String v){//M坐骑id=等级=经验=熟练度
		String[] vs=v.split("=");
		Mount mount=getMount(new BigDecimal(vs[0].substring(1)));
		if (mount!=null) {
			mount.setMountlvl(Integer.parseInt(vs[1]));
			mount.setExp(Integer.parseInt(vs[2]));
			mount.setProficiency(Integer.parseInt(vs[3]));
		}
	}
	public static void LingExp(String v){//L灵宝id=等级=经验=契合
		String[] vs=v.split("=");
		Lingbao lingbao=getLingBao(new BigDecimal(vs[0].substring(1)));
		if (lingbao!=null) {
			lingbao.setLingbaolvl(new BigDecimal(vs[1]));
			lingbao.setLingbaoexe(new BigDecimal(vs[2]));
			lingbao.setLingbaoqihe(Long.parseLong(vs[3]));
		}
	}
	public static void NeiDanExp(String[] vs){//N内丹id=转生=等级=经验
		Goodstable good=GoodsListFromServerUntil.fushis.get(new BigDecimal(vs[0].substring(1)));
		if (good!=null&&good.getType()==750) {
			String[] vvs=good.getValue().split("\\|");
			StringBuffer buffer=new StringBuffer();
			buffer.append(vvs[0]);
			buffer.append("|");
			buffer.append(vvs[1]);
			buffer.append("|内丹等级=");
			buffer.append(vs[1]);
			buffer.append("转");
			buffer.append(vs[2]);
			buffer.append("|经验=");
			buffer.append(vs[3]);
			good.setValue(buffer.toString());
		}
	}
	/**依据经验进行升级判断,内丹升级*/
	public static void IncreaseNedanExp(RoleSummoning pet,Goodstable goodstable,long addexp){
		//判断等级最大能升多少级
		//获取当前等级的最大等级
		String[] strings = goodstable.getValue().split("\\|");
		String[] stringLevel = strings[2].split("\\=");
		String[] stringLevel2 = stringLevel[1].split("\\转");
		String[] stringExp = strings[3].split("\\=");
		
		//当前经验
		long exp = Long.parseLong(stringExp[1])+addexp;
		int nddj = Integer.parseInt(stringLevel2[1]);//内丹等级
		int ndzscs = Integer.parseInt(stringLevel2[0]);//内丹转生次数
		int mostLevel=getNedanMostLevel(ndzscs);	
		 
		int petlvl=AnalysisString.petLvlint(pet.getGrade());	
	    
		if (ndzscs>=pet.getTurnRount()&&nddj>=petlvl) {
			ZhuFrame.getZhuJpanel().addPrompt2("内丹等级不可超过召唤兽等级");
			return;
		}
		if (nddj>=200) {
		    ZhuFrame.getZhuJpanel().addPrompt2("当前内丹已达最大等级！！！");
		    return;
		}
		long maxexp=GetExp.getBBNeiExp(ndzscs, nddj+1);
	    xx:while (exp>=maxexp&&exp>0) {//判断是否最高级最高转
			if (nddj+1>mostLevel) {
				if (ndzscs>=4) {
					ZhuFrame.getZhuJpanel().addPrompt2("当前内丹已达最大等级！！！");
					break xx;
				}else if (ndzscs+1>pet.getTurnRount()) {
					ZhuFrame.getZhuJpanel().addPrompt2("内丹转生次数不可超过召唤兽");
					break xx;
				}else {
					ndzscs++;
					nddj=0;
					maxexp=GetExp.getBBNeiExp(ndzscs, nddj+1);
					exp=0;
				}
			}else if (ndzscs>=pet.getTurnRount()&&nddj+1>petlvl) {
				ZhuFrame.getZhuJpanel().addPrompt2("内丹等级不可超过召唤兽等级");
				break xx;
			}else {
				exp=exp-maxexp;
				nddj++;
				maxexp=GetExp.getBBNeiExp(ndzscs, nddj+1);
			}	
	    }	
	    FrameMessageChangeJpanel.addtext(6, "你的召唤兽"+pet.getSummoningname()+"的"+goodstable.getGoodsname()+"内丹获得"+exp+"经验",null,null);	
	    String newValue = refreshNedan(goodstable.getValue(),ndzscs,nddj,new BigDecimal(exp));
	    goodstable.setValue(newValue);
	    GoodsMouslisten.gooduse(goodstable,2);
		if (FormsManagement.getframe(47).isVisible()) {
			NeidanBtn.ShowNedanMsg(goodstable);
		}
	}
	/**灵宝法宝升级*/
	public static void IncreaseLFExp(Lingbao lingbao,long addexp) throws Exception{
	     int lvl=lingbao.getLingbaolvl().intValue();
	     long exp=lingbao.getLingbaoexe().longValue();
	     long maxexp=ExpUtil.LFExp(lvl);
	     exp+=addexp;
	     StringBuffer buffer=new StringBuffer();
	     buffer.append("你的");
	     buffer.append(lingbao.getBaoname());
	     buffer.append("获得了");
	     buffer.append(ExpUtil.LFExptoString(addexp));
	     buffer.append("道行");
	     FrameMessageChangeJpanel.addtext(6,buffer.toString(),null,null);	
		boolean l=false;
	     while (exp>=maxexp) {
			 if (lvl>=200) {
			    break;	
			 }
			 if (lvl!=0&&lvl%30==0) {
				 exp=maxexp;
				 ZhuFrame.getZhuJpanel().addPrompt2("突破后才可继续升级");
				 break;	
			 }
			 exp-=maxexp;
			 lvl++;
			 maxexp=ExpUtil.LFExp(lvl);
			 l=true;
		 }
	     if (l) {
	    	 StringBuffer buffer1=new StringBuffer();
	 	     buffer1.append("你的");
	 	     buffer1.append(lingbao.getBaoname());
	 	     buffer1.append("升级了");
	 	     ZhuFrame.getZhuJpanel().addPrompt2(buffer1.toString());
		 }
		 lingbao.setLingbaolvl(new BigDecimal(lvl));
		 lingbao.setLingbaoexe(new BigDecimal(exp));
	}
	/**进行属性加点,人物升级*/
	public static void increasePointAndValue(){
		RoleToggleJframe.getRoleToggleJframe().getRoleToggleJpanel().getplayerValue();
		LoginResult loginResult = RoleData.getRoleData().getLoginResult();
		// 进行等级的增加
		loginResult.setGrade(loginResult.getGrade() + 1);
		// 属性加点
		loginResult.setBone(loginResult.getBone() + 1);
		loginResult.setSpir(loginResult.getSpir() + 1);
		loginResult.setPower(loginResult.getPower() + 1);
		loginResult.setSpeed(loginResult.getSpeed() + 1);
		if (loginResult.getTurnAround() >= 4) {
			loginResult.setCalm(loginResult.getCalm() + 1);
		}
		// 进行加点属性变化运算
		PetAddPointMouslisten.sureGetPlayerPoint();
		// 人物的状态回满
		RoleData.getRoleData().getLoginResult().setHp(new BigDecimal(RoleProperty.getHp(RoleData.getRoleData().getLoginResult())));
		RoleData.getRoleData().getLoginResult().setMp(new BigDecimal(RoleProperty.getMp(RoleData.getRoleData().getLoginResult())));
	}	
	/**依据转生等级判断升级的最大等级（人物）*/
	public static int getMostLevel(){
		int mostLevel=0;
		switch (ImageMixDeal.userimg.getRoleShow().getTurnAround()) {
		case 0:
			mostLevel=102;
			break;
        case 1:
        	mostLevel=210;
			break;
        case 2:
        	mostLevel=338;
	        break;
        case 3:
        	mostLevel=459;
	        break;
        case 4:
        	mostLevel=ImageMixDeal.userimg.getRoleShow().getGrade();
	        break;
	    default :
	    	mostLevel=519;	

		}
		return mostLevel;
	}
	/**
	 * 依据转生等级判断升级的最大等级(召唤兽)
	 */
	public static int getPetMostLevel(RoleSummoning pet){
		int mostLevel=0;
		int TurnRount=AnalysisString.petTurnRount(pet.getGrade());
		switch (TurnRount) {
		case 0:
			mostLevel=100;
			break;
        case 1:
        	mostLevel=221;
			break;
        case 2:
        	mostLevel=362;
	        break;
        case 3:
        	mostLevel=543;
	        break;
	    default :
	    	mostLevel=744;	
		}
		return mostLevel;
	}
	
	/**
	 * 依据转生等级判断升级的最大等级(内丹)
	 */
	public static int getNedanMostLevel(int TurnRount){
		int mostLevel=0;
		switch (TurnRount) {
		case 0:
			mostLevel=100;
			break;
        case 1:
        	mostLevel=120;
			break;
        case 2:
        	mostLevel=140;
	        break;
        case 3:
        	mostLevel=170;
	        break;	
        case 4:
        	mostLevel=200;
	        break;	
		}
		return mostLevel;
	}
	//更新内丹信息
	public static String refreshNedan(String values,int turnRount,int grade,BigDecimal exp){
		String[] strings = values.split("\\|");
		String newValue = strings[0]+"|"+strings[1]+"|"+"内丹等级="+turnRount+"转"+grade+"|"+"经验="+exp;
		return newValue;
	}
	/**刷新坐骑面板*/
	public static void showMountValue(Mount mount){		
		mountJPanel mountJPanel=MountJframe.getMountjframe().getMountjpanel();
		int lvl=mount.getMountlvl();
		if (lvl>100) {lvl-=100;}
		//最新的坐骑属性
		int lingxing= (int)Math.floor(mount.getSpri()+(float)lvl/10*mount.getSpri()/2);
		int liliang=(int)Math.floor(mount.getPower()+(float)lvl/10*mount.getPower()/2);
		int gengu=(int)Math.floor(mount.getBone()+(float)lvl/10*mount.getBone()/2);
		//等级
		mountJPanel.getLabmountlevel().setText(mount.getMountlvlString());
		//体力
		mountJPanel.getLabmounttili().setText(mount.getLive()+"");
		//灵性
		mountJPanel.getLabmountintelligence().setText(lingxing+"");
		//力量
		mountJPanel.getLabmountpower().setText(liliang+"");
		//根骨
		mountJPanel.getLabmountRootbone().setText(gengu+"");
		if(lvl < 100){//经验
			mountJPanel.getLabmountexp().setText(mount.getExp()+"/"+GetExp.getMountExp(mount.getMountlvl()));
		}else{//经验
			mountJPanel.getLabmountexp().setText(mount.getExp()+"/999999");
		}
		if(mount.getMoveGrade()==null) {
			mount.setMoveGrade(0);
		}
		//技能熟练度
		MountSkillsJframe.getMountSkillsJframe().getMountSkillsJpanel().getLabProficiency().setText(mount.getProficiency()+"");
	}
	/**ID获取对应PET*/
	public static RoleSummoning getPet(BigDecimal sid){
		for (int i = 0; i < UserMessUntil.getPetListTable().size(); i++) {
			RoleSummoning pet = UserMessUntil.getPetListTable().get(i);
			if (pet.getSid().compareTo(sid)==0) {
				return pet;
			}
		}
		return null;
	}
	/**ID获取对应MOUNT*/
	public static Mount getMount(BigDecimal mid){
		if (ZhuJpanel.getListMount()==null) {
			return null;
		}
		for (int i = 0; i < ZhuJpanel.getListMount().size(); i++) {
			Mount mount = ZhuJpanel.getListMount().get(i);
			if (mount.getMid().compareTo(mid)==0) {
				return mount;
			}
		}
		return null;
	}
	/**ID获取对应LINGBAO*/
	public static Lingbao getLingBao(BigDecimal baoid){
		for (int i = 0; i < RoleLingFa.getRoleLingFa().equipBao.length; i++) {
			Lingbao lingbao=RoleLingFa.getRoleLingFa().equipBao[i];
			if (lingbao!=null&&lingbao.getBaoid().compareTo(baoid)==0) {
				return lingbao;
			}
		}
		return null;
	}
	
	/**
	 * 刷新飞行器面板
     */
    public static void ShouFlyValue(Fly fly){
        AircraftJPanel aircraftJPanel= AircraftJframe.getAircraftJframe().getAircraftJPanel();
        int lvl = fly.getFlylvl();
        if (lvl > 100) {
            lvl -= 100;
        }
        aircraftJPanel.setSkin(fly.getSkin());
        aircraftJPanel.getLabName().setText(fly.getFlyname()+"("+AircraftJPanel.getJieshu(fly.getFlystate())+"阶)");
        aircraftJPanel.getLabLvl().setText(fly.getFlylvlString().toString());
        aircraftJPanel.getLabflytili().setText(fly.getFuel().toString());
        aircraftJPanel.getLabStage().setText(fly.getFlystate().toString());
        if (lvl<100) {
        	aircraftJPanel.getLabExp().setText(fly.getExp()+"/"+GetExp.getFlyExp(fly.getFlylvl()));
        }else {
        	aircraftJPanel.getLabExp().setText(fly.getExp()+"/999999");
        }
    }
}
